package com.yanqu.road.server.cmd.union.cross;

import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.union.UnionProto;
import com.yanqu.road.pb.union.cross.CrossUnionProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.manger.activity.tonbingchannel.ChannelTonBingMgr;
import com.yanqu.road.server.manger.thirdparty.ThirdPartyMgr;
import com.yanqu.road.server.manger.activity.tonbing.TonBingMgr;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.property.Property;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;


@Cmd(code = Protocol.S_UNION_MODIFY_FROM_CROSS, desc = "修改联盟信息")
public class UnionModifyCmdFromCross extends AbstractCommand {


    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {

        CrossUnionProto.UnionModifyCrossRespMsg crossRespMsg = CrossUnionProto.UnionModifyCrossRespMsg.parseFrom(packet.getBytes());

        if(!StringUtils.isNullOrEmpty(crossRespMsg.getConsume())){

            player.getModule(PreReduceModule.class).restorePreReduce(crossRespMsg.getConsume());

            if(crossRespMsg.getRet() == 0){

                player.getModule(CurrencyModule.class).removeCurrency(new Property(GameConfig.GAME_MONEY_INGOTS, BigInteger.valueOf(GameConfig.UNION_CHANGE_NAME_COST)),
                        eLogMoneyType.Union, eLogMoneyType.UnionChangeName);
                if(crossRespMsg.getType() == 0){
                    TonBingMgr.unionChange(player.getUserInfo().getUnionUid());
                    ChannelTonBingMgr.unionChange(player.getUserInfo().getUnionUid());
                }
            }

        }

        if(crossRespMsg.getRet() == 0){
            if(0 == crossRespMsg.getType()){
                ThirdPartyMgr.unionNameReport(crossRespMsg.getUnionUid(), crossRespMsg.getContent());
            }else if(3 == crossRespMsg.getType()){
                ThirdPartyMgr.unionNoticeReport(crossRespMsg.getUnionUid(), crossRespMsg.getContent());
            }
        }
        UnionProto.UnionModifyRespMsg.Builder respMsg = UnionProto.UnionModifyRespMsg.newBuilder();
        respMsg.setRet(crossRespMsg.getRet());
        player.sendPacket(Protocol.U_UNION_MODIFY, respMsg);
    }

    @Override
    public void sendData(GamePlayer player) {

    }
}
